﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Animations;

namespace UFrame.SMB
{
    public class SetParameterExitSMB : StateMachineBehaviour
    {
        public string argName;
        public int argValue;
        public string boolArgName;
        public bool boolValue;
        public bool isExit;
        public bool isUpdate;
        public float ratioTime = 0.5f;
        private bool isOnce = false;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            isOnce = false;
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
        
            if (isExit)
            {
                SetArgs(animator);
            }
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (isUpdate)
            {
                if (stateInfo.normalizedTime > ratioTime && !isOnce)
                {
                    isOnce = false;
                    SetArgs(animator);
                }
            }
        }

        public void SetArgs(Animator animator)
        {
            if (!string.IsNullOrEmpty(argName))
            {
                animator.SetInteger(argName, Convert.ToInt32(argValue));
            }

            if (!string.IsNullOrEmpty(boolArgName))
            {
                animator.SetBool(boolArgName, boolValue);
            }
        }
    }
}